package com.ruoyi.control.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.control.domain.Mainventilator2History;
import com.ruoyi.control.dto.BeltDto;
import com.ruoyi.control.dto.Mainventilator1HistoryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.control.mapper.Mainventilator1HistoryMapper;
import com.ruoyi.control.domain.Mainventilator1History;
import com.ruoyi.control.service.IMainventilator1HistoryService;

/**
 * 主通风机1号风机历史数据Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@Service
public class Mainventilator1HistoryServiceImpl extends ServiceImpl<Mainventilator1HistoryMapper, Mainventilator1History> implements IMainventilator1HistoryService //修改
{
    @Autowired
    private Mainventilator1HistoryMapper mainventilator1HistoryMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询主通风机1号风机历史数据
     *
     * @param id 主通风机1号风机历史数据主键
     * @return 主通风机1号风机历史数据
     */
    @Override
    public Mainventilator1History selectMainventilator1HistoryById(String id) {
        return mainventilator1HistoryMapper.selectMainventilator1HistoryById(id);
    }

    /**
     * 查询主通风机1号风机历史数据列表
     *
     * @param mainventilator1History 主通风机1号风机历史数据
     * @return 主通风机1号风机历史数据
     */
    @Override
    public List<Mainventilator1History> selectMainventilator1HistoryList(Mainventilator1History mainventilator1History) {
        return mainventilator1HistoryMapper.selectMainventilator1HistoryList(mainventilator1History);
    }

    /**
     * 新增主通风机1号风机历史数据
     *
     * @param mainventilator1History 主通风机1号风机历史数据
     * @return 结果
     */
    @Override
    public int insertMainventilator1History(Mainventilator1History mainventilator1History) {
        return mainventilator1HistoryMapper.insertMainventilator1History(mainventilator1History);
    }

    /**
     * 修改主通风机1号风机历史数据
     *
     * @param mainventilator1History 主通风机1号风机历史数据
     * @return 结果
     */
    @Override
    public int updateMainventilator1History(Mainventilator1History mainventilator1History) {
        mainventilator1History.setUpdateTime(DateUtils.getNowDate());
        return mainventilator1HistoryMapper.updateMainventilator1History(mainventilator1History);
    }

    /**
     * 批量删除主通风机1号风机历史数据
     *
     * @param ids 需要删除的主通风机1号风机历史数据主键
     * @return 结果
     */
    @Override
    public int deleteMainventilator1HistoryByIds(String[] ids) {
        return mainventilator1HistoryMapper.deleteMainventilator1HistoryByIds(ids);
    }

    /**
     * 删除主通风机1号风机历史数据信息
     *
     * @param id 主通风机1号风机历史数据主键
     * @return 结果
     */
    @Override
    public int deleteMainventilator1HistoryById(String id) {
        return mainventilator1HistoryMapper.deleteMainventilator1HistoryById(id);
    }

    @Override
    public Integer[] selectMainventilator1HistoryByTagName(String tagName) {
        Integer[] history = new Integer[60 * 24];
        List<Mainventilator1History> historyList = mainventilator1HistoryMapper.selectMainventilator1HistoryByTagName(tagName);
        for (int i = 0; i < 60 * 24; i++) {
            if (i >= historyList.size()) {
                history[i] = 0;
            } else {
                double doubleValue = Double.parseDouble(historyList.get(i).getValue());
                int intValue = (int) doubleValue; // 如果需要四舍五入，可以使用 Math.round(doubleValue)
                history[i] = intValue;
            }
        }
        return history;
    }


    @Override
    public int getMainOneTotal() {
        final String key = "tfOneKey";
        int total = 0;
        String cachedValue = redisCache.getCacheObject(key);

        List<Mainventilator1HistoryDto> list = mainventilator1HistoryMapper.getMainOneTotal();
        total = list.size();
        redisCache.setCacheObject(key, String.valueOf(total));

//        if (cachedValue == null || cachedValue.isEmpty()) {
//            List<Mainventilator1HistoryDto> list = mainventilator1HistoryMapper.getMainOneTotal();
//            total = list.size();
//            redisCache.setCacheObject(key, String.valueOf(total));
//        } else {
//            total = Integer.parseInt(cachedValue);
//            System.out.println("读取tfOneKey缓存: " + total);
//        }
        return Integer.parseInt(cachedValue);
    }
}
